﻿using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using NetFtp.Core.Ftp;

public partial class EditFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USER_LOGIN"] == null)
            Response.Redirect("Default.aspx");

        if (Page.IsPostBack == false)
        {
            fileedit.Value = Request.QueryString["file"];
            FileInfo ftpfile = new FileInfo(fileedit.Value);
            FileInfo localfile = new FileInfo(Path.Combine(Session["FOLDER_SESSION"].ToString(), ftpfile.Name));

            try
            {
                FtpSession ftpSession = new FtpSession(Session["HOST_FTP"].ToString(), int.Parse(Session["PORT_FTP"].ToString()), Session["LOGIN_FTP"].ToString(), Session["MDP_FTP"].ToString(), bool.Parse(Session["PASSIVE_FTP"].ToString()));
                ftpSession.Download(fileedit.Value, localfile.FullName);

                if (localfile.Exists)
                {
                        // Création d'une instance de StreamReader pour permettre la lecture de notre fichier 
                        StreamReader monStreamReader = new StreamReader(localfile.FullName);
                        txtContent.Text = monStreamReader.ReadToEnd();
                        monStreamReader.Close();
                }
                else
                    ClientScript.RegisterStartupScript(this.GetType(), "js1", "<script>alert('Erreur, veuillez rééssayer ulterieurement.');window.close();</script>");
            }
            catch (Exception ex)
            {
                if (localfile.Exists)
                    localfile.Delete();
                ClientScript.RegisterStartupScript(this.GetType(), "js1", "<script>alert(\"Error : " + ex.Message.Replace("'", "\'") + "\");window.close();</script>");
            }
        }
    }

    protected void cmdOk_Click(object sender, EventArgs e)
    {
        FileInfo ftpfile = new FileInfo(fileedit.Value);
        FileInfo localfile = new FileInfo(Path.Combine(Session["FOLDER_SESSION"].ToString(), ftpfile.Name));

        if (localfile.Exists)
        {
            try
            {
                // Création d'une instance de StreamReader pour permettre la lecture de notre fichier 
                StreamWriter monStreamWriter = new StreamWriter(localfile.FullName);
                monStreamWriter.Write(txtContent.Text);
                monStreamWriter.Close();

                FtpSession ftpSession = new FtpSession(Session["HOST_FTP"].ToString(), int.Parse(Session["PORT_FTP"].ToString()), Session["LOGIN_FTP"].ToString(), Session["MDP_FTP"].ToString(), bool.Parse(Session["PASSIVE_FTP"].ToString()));
                ftpSession.Upload(localfile.FullName, fileedit.Value);

                localfile.Delete();

                ClientScript.RegisterStartupScript(this.GetType(), "js1", "<script>window.close();</script>");
            }
            catch (Exception ex)
            {
                if (localfile.Exists)
                    localfile.Delete();
                ClientScript.RegisterStartupScript(this.GetType(), "js1", "<script>alert(\"Error : " + ex.Message.Replace("'", "\'") + "\");window.close();</script>");
            }
        }
        else
            ClientScript.RegisterStartupScript(this.GetType(), "js1", "<script>alert('Error : File not exist.');</script>");
    }

    protected void cmdCancel_Click(object sender, EventArgs e)
    {
        FileInfo ftpfile = new FileInfo(fileedit.Value);
        FileInfo localfile = new FileInfo(Path.Combine(Session["FOLDER_SESSION"].ToString(), ftpfile.Name));

        if (localfile.Exists)
            localfile.Delete();

        ClientScript.RegisterStartupScript(this.GetType(), "js1", "<script>window.close();</script>");
    }
}
